﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using Aspose.Pdf;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Org.BouncyCastle.Asn1.Ocsp;

//using Aspose.Pdf;

namespace WeeklyEmailReporting
{
	public class Html2Pdf
	{
		public static Pdf Convert(string html)
		{
			Pdf pdf = new Pdf();
			Aspose.Pdf.License apLic = new Aspose.Pdf.License();
			apLic.SetLicense("Aspose.Pdf.lic");

			using (var htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html.Replace("\r", "").Replace("\n", "").Replace("\t", ""))))
			{
				pdf = new Pdf();
				/*Aspose.Pdf.Section section = pdf.Sections.Add();

				Aspose.Pdf.Text text = new Text(section, html.Replace("\r", "").Replace("\n", "").Replace("\t", ""));

				text.IsHtmlTagSupported = true;

				section.Paragraphs.Add(text);*/


				//pdf.HtmlInfo.ImgUrl = "~/Images";
				try
				{
					pdf.BindHTML(htmlStream);
				}
				catch(Exception ex)
				{
					
				}
				//pdf.Save("D:/report.pdf");
			}

			return pdf;
		}

		public static Pdf ConvertItext(string html)
		{

			Document document = new Document();

			PdfWriter.GetInstance(document, new FileStream("D:/Chap01011.pdf", FileMode.Create));
			document.Open();
			//iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(string.Format("http://{0}:{1}{2}/{3}", HttpContext.Current.Request.Url.Host, HttpContext.Current.Request.Url.Port, HttpContext.Current.Request.ApplicationPath, "EmailTemplates/logo.jpg"));

			//pdfImage.ScaleToFit(102, 103);

			//pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(180, 760);

			//document.Add(pdfImage);
			iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
			iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
			hw.Parse(new StringReader(html.Replace("\r", "").Replace("\n", "").Replace("\t", "")));
			document.Close();
			//ShowPdf("Chap0101.pdf");

			return new Pdf();
		}

	}
}
